package com.jiangnan.service;

import com.alibaba.fastjson.JSONObject;
import com.jiangnan.model.CleanData;
import com.jiangnan.utils.CollectionUtils;
import com.jiangnan.utils.ExcelExporter;
import com.jiangnan.utils.FastjsonUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 *
 * @author chenliang
 * @email wschenliang@aliyun.com
 */
@Service
public class FileService implements IFileService {

    /*
     * 一个设备统计200个特征向量，进行下一个设备统计
     *
     * 统计内容：
     * F00：payload_len>500的包个数
     * F01：dns包个数
     * F02：ICMP_IGMP包个数
     * F03：NTP包个数
     * F04：HTTP包个数
     * F05：UDP_QQ包个数 （此字段统计了，但在训练中未使用）
     * F06-F19：为保留字段，暂未使用
     * F20-F29：不同域名查询dns包分别统计
     * F30-F39：payload_len>500的包，不同包类型和包大小分别统计
     * CLASS：设备类别
     *
     */

    @Override
    public byte[] convertJson2Excel(byte[] fileBytes) {
        String data = new String(fileBytes);
        String[] items = data.split("\n");
        if (CollectionUtils.isEmpty(Arrays.asList(items))) {
            //空数据
            return null;
        }
        //创建二维数据放数据
        String[][] array = new String[items.length][41];

        for (int i = 0; i < items.length; i++) {
            JSONObject jsonObject = JSONObject.parseObject(items[i]);
            Integer payloadLen = FastjsonUtils.get(jsonObject, int.class, "payload_len");
            
            array[i][0] = "0";
            array[i][1] = "0";
            array[i][2] = "0";
            array[i][3] = "0";
            array[i][4] = "0";
            array[i][5] = "0";
            array[i][6] = "0";
            array[i][7] = "0";
            array[i][8] = "0";
            array[i][9] = "0";
            array[i][10] = "0";
            array[i][11] = "0";
            array[i][12] = "0";
            array[i][13] = "0";
            array[i][14] = "0";
            array[i][15] = "0";
            array[i][16] = "0";
            array[i][17] = "0";
            array[i][18] = "0";
            array[i][19] = "0";
            array[i][20] = "0";
            array[i][21] = "0";
            array[i][22] = "0";
            array[i][23] = "0";
            array[i][24] = "0";
            array[i][25] = "0";
            array[i][26] = "0";
            array[i][27] = "0";
            array[i][28] = "0";
            array[i][29] = "0";
            array[i][30] = "0";
            array[i][31] = "0";
            array[i][32] = "0";
            array[i][33] = "0";
            array[i][34] = "0";
            array[i][35] = "0";
            array[i][36] = "0";
            array[i][37] = "0";
            array[i][38] = "0";
            array[i][39] = "0";
            array[i][40] = "0";
        }
        ExcelExporter excelExporter = new ExcelExporter(CleanData.header());
        return excelExporter.export("数据清洗", array);
    }
}
